package com.learn.sb.caffeine.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@RestController
@Slf4j
public class MsgController {

	public Map<String, Object> resultMap(String code, String msg, Object data) {
		Map<String, Object> map = new HashMap<>();
		map.put("code", code);
		map.put("msg", msg);
		map.put("data", data);
		return map;
	}

	private ConcurrentHashMap cacheHashMap;

	@PostMapping("/jzjy-session/sendmsg")
	public Mono<Map<String, Object>> sendJzjySessionMsg(@RequestBody Map<String, Object> msg) {

		try {
			long startNs = System.nanoTime();
			cacheHashMap.putAll(msg);
			long endNs = System.nanoTime();
			String time = (endNs-startNs) + "ns.";
			log.info("msg-web jzjy revc msg : " + time);
			return Mono.just(resultMap("0", "success", time));
		} catch (Exception e) {
			log.error(msg.toString(), e);
			return Mono.just(resultMap("-1", "failed", e.getMessage().toString()));
		}
	}

	@PostMapping("/rzrq-session/sendmsg")
	public Mono<Map<String, Object>> sendRzrqSessionMsg(@RequestBody Map<String, Object> msg) {
		try {
			long startNs = System.nanoTime();
			for (int i = 0; i < 10000; ++i) {
				cacheHashMap.put("rzrq1key" + i, "msg" + i);
			}
			long endNs = System.nanoTime();
			String time = (endNs-startNs) + "ns.";
			log.info("msg-web rzrq revc msg : " + time);
			return Mono.just(resultMap("0", "success", time));
		} catch (Exception e) {
			log.error(msg.toString(), e);
			return Mono.just(resultMap("-1", "failed", e.getMessage().toString()));
		}
	}


}
